import { httpFetch } from '../../utils/req'

const headers = {
  Cookie: '',
  Authorization: 'haut.hnscen.cn',
  Host: 'api.jinkex.com',
  Referer: 'https://haut.hnscen.cn/',
  Origin: 'https://haut.hnscen.cn',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0',
  'Sec-Fetch-Dest': 'empty',
  'Sec-Fetch-Mode': 'cors',
  'Sec-Fetch-Site': 'cross-site',
  'Content-Type': 'application/json;charset=utf-8'
}

const url = 'https://api.jinkex.com/exam/v1/user/enjoin'

export default (query, request) => {
  return new Promise((resolve, reject) => {
    const testId = query.testid
    const token = query.token
    if (testId && token) {
      const data = {
        school_host: 'haut.hnscen.cn',
        testId:	testId,
        token: token
      }
      httpFetch(url, {
        method: 'post',
        body: data,
        headers: headers
      }
      ).promise.then(res => {
        resolve(res.body)
      }).catch(e => {
        reject(e)
      })
    } else {
      reject({
        name: '错误',
        message: '考试ID或TOKEN缺失'
      })
    }
  })
}
